﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-8-10
Description:SenderMoney.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;

public partial class SenderMoney : System.Web.UI.Page
{
    string TableName = "SenderMoney";//表名
    static string SqlWhere = " 1=1 ";//过滤条件
    string GetCountSql = "select count(id)  from SenderMoney where " + SqlWhere;
    string SqlTable = "select *  from SenderMoney where " + SqlWhere + "  order by id desc ";
    string ReturnPage = "SenderMoney.aspx";
    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化模型
    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        if (!IsPostBack)
        {
            string act = FTchina.basic.FilterSQL(Request["act"]);
            switch (act)
            {
                case "mod":
                    {
                        btnSenderMoneyOk.Text = "确定修改";
                        txbid.Enabled = false;
                        DisplayData();
                        plop.Visible = true;
                        break;
                    }
                //case "del":
                //    {
                //        DeleteData();
                //        break;
                //    }
                //case "add":
                //    {
                //        txbid.Text = "0";
                //        txbid.Visible = false;
                //        plop.Visible = true;//添加修改的表单可以看见
                //        break;
                //    }
                default:
                    {
                        txbid.Text = "0";
                        txbid.Visible = false;
                        plop.Visible = false;//添加修改的表单看不见
                        break;
                    }

            }
            DdlBind();

            if (qm.WorkPage != ReturnPage)
            {
                qm.TableName = TableName;
                qm.GetCountSql = GetCountSql;
                qm.GetTableSql = SqlTable;
                qm.ReturnPage = ReturnPage;
                qm.SqlWhere = SqlWhere;
                qm.WorkPage = ReturnPage;

            }

            bindData(qm);
        }
    }

    protected void DdlBind()
    {
        string sql = "select * from senderlist order by id desc";
        ddlsenderid.DataSource = shujuku.GetDataTable(sql);
        ddlsenderid.DataTextField = "sendername";
        ddlsenderid.DataValueField = "senderid";
        ddlsenderid.DataBind();
    }

    private void DeleteData()
    {
        FTchina.UImsg.PageRender(FTchina.BLL.SenderMoneyManager.DeleteSenderMoneyByid(Convert.ToInt32(FTchina.basic.FilterSQL(Request["id"]))) > 0, HttpContext.Current.Request.Path.ToString());
    }
    private void DisplayData()
    {
        FTchina.Models.SenderMoney mSenderMoney = FTchina.BLL.SenderMoneyManager.GetSenderMoneyByid(Convert.ToInt32(FTchina.basic.FilterSQL(Request["id"])));

        txbid.Text = mSenderMoney.Id.ToString();

        txbsended.Text = mSenderMoney.Sended.ToString();

        txbmoney.Text = mSenderMoney.Money.ToString();


        ddlsenderid.SelectedValue = mSenderMoney.SenderID;

    }

    private void DataModifiy()
    {
        FTchina.Models.SenderMoney mSenderMoney = new FTchina.Models.SenderMoney();

        mSenderMoney.Id = Convert.ToInt32(FTchina.basic.FilterSQL(txbid.Text));
        mSenderMoney.Money = Convert.ToDecimal(FTchina.basic.FilterSQL(txbmoney.Text));
        mSenderMoney.Sended = Convert.ToInt32(FTchina.basic.FilterSQL(txbsended.Text));
        mSenderMoney.SenderID = ddlsenderid.SelectedValue;

        bool flag=FTchina.BLL.SenderMoneyManager.ModifySenderMoneyByid(mSenderMoney) > 0;
        //修改账户金额
        if (flag)
        {
            FTchina.Models.SenderMoney_Log mSenderMoney_Log = new FTchina.Models.SenderMoney_Log();
            mSenderMoney_Log.Id = 0;
            mSenderMoney_Log.SenderID = mSenderMoney.SenderID;
            mSenderMoney_Log.Money = mSenderMoney.Money;
            mSenderMoney_Log.Date = DateTime.Now;
            mSenderMoney_Log.Memo = "后台修改";
            mSenderMoney_Log.Direction = FTchina.SMSServer.SMSServer.Direction.ModByMan.ToString();
            mSenderMoney_Log.Smsguid = string.Empty;
            mSenderMoney_Log.Oper = FTchina.Library.MasterHelper.MasterName;
            FTchina.UImsg.PageRender(FTchina.BLL.SenderMoney_LogManager.AddSenderMoney_Log(mSenderMoney_Log) > 0);

        }
        



    }
    private void DataAdd()
    {
        FTchina.Models.SenderMoney mSenderMoney = new FTchina.Models.SenderMoney();

        mSenderMoney.Id = Convert.ToInt32(FTchina.basic.FilterSQL(txbid.Text));

        mSenderMoney.SenderID = ddlsenderid.SelectedValue;

        mSenderMoney.Money = Convert.ToDecimal(FTchina.basic.FilterSQL(txbmoney.Text));

        mSenderMoney.Sended = 0;


        FTchina.UImsg.PageRender(FTchina.BLL.SenderMoneyManager.AddSenderMoney(mSenderMoney) > 0);
    }


    protected void btnSenderMoneyOk_Click(object sender, EventArgs e)
    {
        if (btnSenderMoneyOk.Text == "确定修改") DataModifiy();
        else DataAdd();
    }




    #region gvList显示

    private void bindData(FTchina.SearchQuery.QueryModel qm)
    {

        int pCurrentPage = (FTchina.basic.SafeRequest(Request["page"]).Length > 0) ? Convert.ToInt32(FTchina.basic.SafeRequest(Request["page"])) : 1;
        int pageSize = 15;//每页的数据
        int AllCount = Convert.ToInt32(FTchina.shujuku.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
        litpager.Text = FTchina.Api.Pager.GetPagerWithPageName(qm.GetTableSql, AllCount, pCurrentPage, pageSize, qm.ReturnPage);
        gvList.DataSource = FTchina.shujuku.ShowForPage(qm.TableName, qm.SqlWhere, pCurrentPage, pageSize);
        gvList.DataBind();

    }
    #endregion

    #region GridView等控件导出到Excel
    protected void btnexcel_Click(object sender, EventArgs e)
    {
        //将分页去除
        gvList.Width = Unit.Percentage(100);
        gvList.AutoGenerateColumns = false;
        gvList.AllowPaging = false;
        string GetDateSql = "select * from SenderMoney  order by id desc";
        DataSet ds = FTchina.shujuku.GetDataSet(GetDateSql);
        gvList.DataSource = ds;
        gvList.DataBind();

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(stringWrite);
        gvList.RenderControl(writer);

        FTchina.Api.ToExcel.HtmlTableToExcel(stringWrite.ToString());//去除了input字符，其他的字符也可以加，正则
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // 覆盖VerifyRenderingInServerForm方法，非常重要，否则系统会报错
        //base.VerifyRenderingInServerForm(control);
    }
    #endregion


}






